package wangguolongTest;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.base.BaseRecyclerAdapter;
import com.example.znbk.znbklibrary.base.BaseRecyclerViewHolder;
import com.example.znbk.znbklibrary.util.LancooUtils;
import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.beans.FirstPageBean;
import com.lancoo.znbkxx.uis.PhoneMainActivity;

import org.w3c.dom.Text;

import znbkBkfx.BkfxMainInforActivity;

public class BkfxItemAdapter extends BaseRecyclerAdapter {


    private Context context;
    private FirstPageBean firstPageEntity;
    private LayoutInflater inflater;
    public BkfxItemAdapter(Context context,FirstPageBean firstPageEntity){
        this. context=context;
        this.firstPageEntity = firstPageEntity;
        inflater=LayoutInflater. from(context);
    }

    @Override
    public View getHolderView(ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.item_bkfx_answered,parent, false);
        return view;
    }

    private LinearLayout mLlItemClick;
    private LinearLayout mLlPaperState;
    private TextView mTvStuScore;
    private TextView mTvStuZj;
    private TextView mTvPaperName;
    private TextView mTvPaperUseTime;
    private TextView mTvPaperScore;
    private RelativeLayout mRlItemShowPaper;
    private TextView mTvItemShowPaper;
    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder baseRecyclerViewHolder, final int position) {
        mLlItemClick = baseRecyclerViewHolder.getView(R.id.ll_itemClick);
        mLlItemClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhoneMainActivity.setPaperId(firstPageEntity.getPaperList().get(position).getPaperID());
                Intent intent = new Intent();
                intent.setClass(context, BkfxMainInforActivity.class);
                context.startActivity(intent);
            }
        });

        mTvStuScore = baseRecyclerViewHolder.getView(R.id.tv_stuScore);
        float Score =  firstPageEntity.getPaperList().get(position).getScore();
        String strScore = String.valueOf(Score);
        if(strScore.equals("0.0")){
            strScore = "0";
        }
        mTvStuScore.setText(Html.fromHtml(context.getResources().
                getString(R.string.paper_stu_score,strScore)));

        mTvStuZj = baseRecyclerViewHolder.getView(R.id.tv_stuZj);
        int TotalStuCount = firstPageEntity.getPaperList().get(position).getTotalStuCount();
        int Rank = firstPageEntity.getPaperList().get(position).getRank();
        double fZj= LancooUtils.getCorrectRate((TotalStuCount-Rank+1),TotalStuCount);
        String strZj = LancooUtils.getPercentInstance(fZj);
        mTvStuZj.setText(Html.fromHtml(context.getResources().
                getString(R.string.paper_zj,strZj)));

        mTvPaperName = baseRecyclerViewHolder.getView(R.id.tv_paperName);
        mTvPaperName.setText(firstPageEntity.getPaperList().get(position).getPaperName());

        mTvPaperUseTime = baseRecyclerViewHolder.getView(R.id.tv_paperUseTime);
        int CountDownTime = firstPageEntity.getPaperList().get(position).getCountDownTime();

        String strDays = LancooUtils.getDays(CountDownTime);
        int Days = Integer.valueOf(strDays);
        if(Days>0&&Days<=9){
            strDays = "0"+strDays;
        }

        String strHour = LancooUtils.getHours(CountDownTime);
        int Hour = Integer.valueOf(strHour);
        if(Hour>=0&&Hour<=9){
            strHour = "0"+strHour;
        }
        String strMins = LancooUtils.getMins(CountDownTime);
        int Mins = Integer.valueOf(strMins);
        if(Mins>=0&&Mins<=9){
            strMins = "0"+strMins;
        }
        String strSeconds = LancooUtils.getSeconds(CountDownTime);
        int Seconds = Integer.valueOf(strSeconds);
        if(Seconds>=0&&Seconds<=9){
            strSeconds = "0"+strSeconds;
        }
        if(Days>0){
            mTvPaperUseTime.setText(Html.fromHtml(context.getResources().
                    getString(R.string.paper_stu_use_time_big, strDays, strHour, strMins)));
        }else {
            mTvPaperUseTime.setText(Html.fromHtml(context.getResources().
                    getString(R.string.paper_stu_use_time_small, strHour, strMins, strSeconds)));
        }

        mTvPaperScore = baseRecyclerViewHolder.getView(R.id.tv_paperScore);

        mRlItemShowPaper = baseRecyclerViewHolder.getView(R.id.rl_itemShowPaper);
        mTvItemShowPaper = baseRecyclerViewHolder.getView(R.id.tv_itemShowPaper);
        Log.e("AAA","========试卷时间======"+LancooUtils.formatSeconds(CountDownTime));

        mLlPaperState = baseRecyclerViewHolder.getView(R.id.ll_paperState);
        int State = firstPageEntity.getPaperList().get(position).getState();
        if(State==2||State==3){
            mLlPaperState.setBackgroundResource(R.mipmap.image_paper_answered);
            mTvStuScore.setVisibility(View.VISIBLE);
            mTvStuZj.setVisibility(View.VISIBLE);

            float AverageScore = firstPageEntity.getPaperList().get(position).getAverageScore();
            String strAverageScore = String.valueOf(AverageScore);
            float HighestScore = firstPageEntity.getPaperList().get(position).getHighestScore();
            String strHighestScore = String.valueOf(HighestScore);
            mTvPaperScore.setText(Html.fromHtml(context.getResources().
                    getString(R.string.paper_score_answered,strAverageScore,strHighestScore)));

            mRlItemShowPaper.setBackgroundResource(R.drawable.rectangle_radius_paper_item_blue);
            mTvItemShowPaper.setText("查看详情");
            mTvItemShowPaper.setTextColor(Color.parseColor("#09c2f5"));

        }else{

            int SubmitStuCount = firstPageEntity.getPaperList().get(position).getSubmitStuCount();
            float HighestScore = firstPageEntity.getPaperList().get(position).getHighestScore();
            String strHighestScore = String.valueOf(HighestScore);

            if(State==1){
                mTvPaperScore.setText(Html.fromHtml(context.getResources().
                        getString(R.string.paper_score_never_answer,SubmitStuCount,TotalStuCount,strHighestScore)));
                mRlItemShowPaper.setBackgroundResource(R.drawable.rectangle_radius_paper_item_yellow);
                mTvItemShowPaper.setText("继续作答");
                mTvItemShowPaper.setTextColor(Color.parseColor("#ff8203"));
            }else if(State==0){
                mTvPaperScore.setText(Html.fromHtml(context.getResources().
                        getString(R.string.paper_score_never_answer,SubmitStuCount,TotalStuCount,strHighestScore)));
                mRlItemShowPaper.setBackgroundResource(R.drawable.rectangle_radius_paper_item_yellow);
                mTvItemShowPaper.setText("开始作答");
                mTvItemShowPaper.setTextColor(Color.parseColor("#ff8203"));
            }
            mLlPaperState.setBackgroundResource(R.mipmap.image_paper_never_answered);
            mTvStuScore.setVisibility(View.GONE);
            mTvStuZj.setVisibility(View.GONE);

        }

    }

    @Override
    public int getItemCount() {
        return firstPageEntity.getPaperList().size();
    }


}
